#####
# Replication for: "Can Political Speech Foster Tolerance of Immigrants?" by Schleiter, Tavits, and Ward.
# Table S.16
#####

library(here)
library(data.table)
library(texreg)

# source the custom functions
source("functions.R")

# load the pooled data if not already in workspace
if(!exists("pooled")){
  pooled <- fread(here("data", "pooled.csv"))
}

# define the imbalanced covaraites
imbMain <- c("age", "ageSq")

# Fitting -----------------------------------------------------------------

s16_1 <- noCov(second_order_beliefs2, chatty = F)
s16_2 <- allCov(second_order_beliefs2, chatty = F)
s16_3 <- flexCov(second_order_beliefs2, covs = imbMain, chatty = F)

s16_4 <- noCov(prejudice_discomfort2, chatty = F)
s16_5 <- allCov(prejudice_discomfort2, chatty = F)
s16_6 <- flexCov(prejudice_discomfort2, covs = imbMain, chatty = F)


# Reporting ---------------------------------------------------------------

# in the document, these three outputs are merged to a single latex table.

texregPS(
  list(s16_1, s16_2, s16_3),
  custom.model.names = paste0("(", 1:3, ")"),
  custom.gof.rows = list(
    "All Covariates" = c("", "$\\checkmark$", ""),
    "Imbalanced Covaraites" = c("", "", "$\\checkmark$")
  )
)

texregPS(
  list(s16_4, s16_5, s16_6),
  custom.model.names = paste0("(", 4:6, ")"),
  custom.gof.rows = list(
    "All Covariates" = c("", "$\\checkmark$", ""),
    "Imbalanced Covaraites" = c("", "", "$\\checkmark$")
  )
)
